// FUNCTIONS.JS
// Utility functions that Ext JS does not provide, for use throughout the
// application.
// ----------------------------------------------------------------------------

Ext.define('Util.Functions', {}); // Placeholder, ignore this.

window.Functions =
{

    SERVICE_URL : '/',

    GET : 'GET',
    POST : 'POST',
    jsonCommand : function(method, url, params, callbacks)
    {
        var me = this;
        Ext.Ajax.request(
        {
            url : Functions.SERVICE_URL + url,
            method : method,
            params : params,
            success : function(result)
            {
                var json = Ext.decode(result.responseText);
                if (json.success === false)
                {
                    if (callbacks.failure)
                    {
                        callbacks.failure(json);
                    } else
                    {
                        Functions.errorMsg("<b>Error occurred in url '" + url + "':</b><br /><br />" + "<code>" + json.errorClass + "</code><br /><br />" + json.error, "Command Error");
                    }
                } else
                {
                    if (callbacks.success)
                        callbacks.success(json.response);
                }
            },
            failure : function(result)
            {
                if (callbacks.failure)
                {
                    callbacks.failure(
                    {
                        success : false,
                        error : result.statusText,
                        errorClass : "HTTP " + result.status
                    });
                } else
                {
                    Functions.errorMsg("<b>Failed to send url '" + url + "':</b><br /><br />" + "<code>HTTP " + result.status + " - " + result.statusText + "</code><br /><br />" + "Command Error");
                }
            }
        });
    },

    // Displays an Ext JS error message popup.
    errorMsg : function(msg, title)
    {
        var message = msg;
        // Check if 'msg' is an error object.
        if (!msg)
        {
            message = "(null message)";
        } else if (msg.message)
        {
            try
            {
                if (!title)
                    title = msg.constructor.name;
            } catch (e)
            {
            }
            message = msg.message;
        }
        title = title || "Error";
        Ext.MessageBox.show(
        {
            title : title,
            msg : message,
            buttons : Ext.MessageBox.OK,
            icon : Ext.MessageBox.ERROR
        });
        if (Log)
            Log.error(msg);
    },

    isEmpty : function(value)
    {
        return '' === value || undefined === value || null === value;
    },

    isEqual : function(obj1, obj2)
    {
        var check = function(o1, o2)
        {
            var key;
            for (key in o1)
            {
                if (o1.hasOwnProperty(key))
                {
                    if (!equals(o1[key], o2[key]))
                        return false;
                }
            }
            return true;
        };

        var equals = function(obj1, obj2)
        {
            if (typeof obj1 == 'undefined' || obj1 === null)
                return obj1 === obj2;
            if (typeof obj2 == 'undefined' || obj2 === null)
                return obj1 === obj2;
            // Short circuit if the same object is passed twice
            if (obj1 === obj2)
                return true;
            if (Ext.isDate(obj1))
                if (Ext.isDate(obj2))
                    return obj1.getTime() == obj2.getTime();
                else
                    return false;
            if (Ext.isObject(obj1))
            {
                if (Ext.isObject(obj2))
                    return check(obj1, obj2) && check(obj2, obj1);
                else
                    return false;
            } else if (Ext.isArray(obj1))
            {
                if (obj2 === null)
                    return false;
                if (obj1.length != obj2.length)
                    return false;
                for (var i = 0; i < obj1.length; i++)
                    if (!Functions.isEqual(obj1[i], obj2[i]))
                        return false;
                return true;
            }
            return false;
        };
        return equals(obj1, obj2);
    },

    getAllDigites : function(inputValue)
    {
        var digites = "";
        for (var i = 0; i < inputValue.length; i++)
        {
            if (inputValue.charCodeAt(i) >= 48 && inputValue.charCodeAt(i) <= 57)
            {
                digites = digites + inputValue.substr(i, 1);
            }
        }
        return digites;
    },

    validateEmail : function(email)
    {
        return Ext.form.field.VTypes.email(email);
    },

    openUrl : function(obj)
    {
        var url = obj.url;
        var inNewTab = Functions.isEmpty(obj.newTab) ? true : obj.newTab;
        var target = '';
        if (inNewTab)
        {
            target = '_blank';
        }
        target = window.open(url, target);
    },

    undefinedToEmpty : function(value)
    {
        var result = value;
        if (Functions.isEmpty(value))
        {
            result = '';
        }
        return result;
    }
};
